package horncomb.bpm.process.instance;

import org.springframework.util.StringUtils;

import horncomb.bpm.BpmContextHolder;
import horncomb.bpm.process.core.NestedToken;
import horncomb.bpm.process.core.Token;
import horncomb.bpm.process.core.entity.NestedTokenEntity;

/**
 * @author Daniel
 */
public class DefaultNestedToken extends DefaultToken implements NestedToken,
		NestedTokenEntity {
	private static final long serialVersionUID = 465910660675095516L;

	public DefaultNestedToken() {
		super();
	}

	public DefaultNestedToken(NestedTokenEntity entity) {
		super(entity);
	}

	public DefaultNestedToken(NestedTokenEntity entity, String parentId) {
		super(entity);
		this.getEntity().setParentId(parentId);
	}

	public DefaultNestedToken(NestedTokenEntity entity, String parentId,
			String processInstanceId) {
		super(entity, processInstanceId);
		this.getEntity().setParentId(parentId);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public NestedTokenEntity getEntity() {
		return (NestedTokenEntity) super.getEntity();
	}

	public Token getParent() {
		if (StringUtils.hasText(this.getEntity().getParentId())) {
			return BpmContextHolder.getContext().getProcessManagementService()
					.findToken(this.getEntity().getParentId());
		} else {
			return null;
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public String getParentId() {
		return this.getParentId();
	}

	/**
	 * {@inheritDoc}
	 */
	public void setParentId(String parentId) {
		this.getEntity().setParentId(parentId);
	}
}
